<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Shop;
use DB;

class ShopController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //搜索条件
        $where = '';
        $ob = DB::table('shop');
        //判断是否有搜索条件商户名
        if($request->has('shop_name')){
            //获取搜索的条件
            $shop_name = $request->input('shop_name');
            //添加到将要携带到分页中的数组中
            $where['shop_name'] = $shop_name;
            //给查询添加where条件 查询的格式
            $ob->where('shop_name', 'like',"%{$shop_name}%"); 
        }
        //判断是否有商店名
        if($request->has('store_name')){
            $store_name = $request->input('store_name');
            $where['store_name'] = $store_name;
            $ob->where('store_name', 'like',"%{$store_name}%"); 
        }
        $shops = $ob->paginate(3);
        //获得总数
        $count = $shops->total();  
        return view('admin.shop_index')->with('shops',$shops)->with('count',$count)->with('where',$where);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.shop_add');
    }

    /**
     * Store a newly created resource in storage.
     *商店添加
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {  
        //字段验证
        $this->validate($request, [
        'img' => 'required',
        'shop_name' => 'required|max:255',
        'store_name' => 'required',
        'shop_phone' => 'required|size:11',
        'shop_pwd' => 'required',
        'shop_email' => 'required|email',
        'shop_description' => 'required',
        ]);
        $img = $request->file("img");
        if($request->hasFile("img")){
            //确认上传的文件是否成功
            if($img->isValid()){
                $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $name = time().rand(1000,9999).".".$ext;
                //移动文件的路径
                $img->move("upload/",$name);  
                $url =  "/upload/".$name;
                $shop = new Shop();
                $shop->shop_logo = $url;
                $shop->shop_name = $request->shop_name;
                $shop->store_name = $request->store_name;
                $shop->shop_phone = $request->shop_phone;
                $shop->shop_pwd = md5($request->shop_pwd);
                $shop->shop_email = $request->shop_email;
                $shop->shop_description = $request->shop_description;
                $res = $shop->save();
                return redirect('admin/shop');    
            }
        }
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $shops = Shop::where('shop_id',$id)->get();
        return view('admin.shop_edit')->with('shops',$shops);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id 删除旧照片
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $shops = Shop::where('shop_id',$id)->get();
        $img = $request->file("img");
        if($request->hasFile("img")){
            //确认上传的文件是否成功
            if($img->isValid()){
                $ext = $img->getClientOriginalExtension(); //获取上传文件名的后缀名
                //执行移动上传文件
                $name = time().rand(1000,9999).".".$ext;
                //移动文件的路径
                $img->move("upload/",$name);  
                $url =  "upload/".$name; 
                //删除旧照片
                unlink($shops[0]->shop_logo);
            }
        }else{
          $url = $shops[0]->shop_logo;
        }
        $res = Shop::where('shop_id', $id)
            ->update([
           'shop_logo' => $url,
           'shop_name' => $request->shop_name,
           'store_name' => $request->store_name,
           'shop_phone' => $request->shop_phone,
           'shop_pwd' => md5($request->shop_pwd),
           'shop_email' => $request->shop_email,
           'shop_description' => $request->shop_description,]
            );
        return redirect('/admin/shop');
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *状态改变
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function status(Request $request){
        $id = $request->id;
        $status = $request->status;
        $res = Shop::where('shop_id',$id)->update(['shop_status'=>!$status]);
        if($res && $status){
            $data['error'] = '关闭'; 
        }else{
            $data['error'] = '正常营业';  
        }
        return json_encode($data);
    }

    /**
     * Remove the specified resource from storage.
     *删除商家
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */

    public function del(Request $request){
        $id = $request->id;
        return Shop::where('shop_id',$id)->delete();
    }

    /**
     * Remove the specified resource from storage.
     *批量删除商家
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function doDel(Request $request){
        $id = $request->id;
        $str = explode(",",$id);
        foreach($str as $v){
            DB::table('shop')->where('shop_id',"=","$v")->delete();
        }
            return  redirect("admin/shop");
        }



}
